<template>
  <!--弹窗-->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    width="500px"
  >
    <el-form
      :rules="rules"
      ref="formDataRef"
      :model="formData"
      label-width="100px"
      style="width: 400px"
      status-icon
    >
      <el-form-item label="用户名：" prop="username">
        <el-input v-model="formData.username" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="昵称：" prop="nickName">
        <el-input v-model="formData.nickName" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="手机号：" prop="mobile">
        <el-input v-model="formData.mobile" maxlength="11"> </el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="formData.email"> </el-input>
      </el-form-item>
      <el-form-item label="帐号过期：" prop="isAccountNonExpired">
        <el-radio-group v-model="formData.isAccountNonExpired">
          <el-radio :label="1" border>未过期</el-radio>
          <el-radio :label="0" border>已过期</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="密码过期：" prop="isCredentialsNonExpired">
        <el-radio-group v-model="formData.isCredentialsNonExpired">
          <el-radio :label="1" border>未过期</el-radio>
          <el-radio :label="0" border>已过期</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="帐号锁定：" prop="isAccountNonLocked">
        <el-radio-group v-model="formData.isAccountNonLocked">
          <el-radio :label="1" border>未锁定</el-radio>
          <el-radio :label="0" border>已锁定</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item align="right">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button size="mini" type="primary" @click="submitForm"
          >确 定
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { ApiPosUser, ApiPutUserEdit } from '@/api/user'
export default {
  props: {
    dialogVisible: { //弹出隐藏
      type: Boolean,
      default: false
    },
    title: { // 标题
      type: String,
      default: ''
    },
    formData: { // 表单数据
      type: Object,
      default: {}
    },
    remoteForm: Function // 用于关闭窗口
  },
  data () {
    return {
      // 校验表单
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: 'blur' }
        ],

        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],

        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],

        isAccountNonExpired: [
          { required: true, message: '请选择', trigger: 'change' }
        ],

        isCredentialsNonExpired: [
          { required: true, message: '请选择', trigger: 'change' }
        ],

        isAccountNonLocked: [
          { required: true, message: '请选择', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose () {
      this.remoteForm()
    },
    //提交表单
    submitForm (formName) {
      this.$refs['formDataRef'].validate(valid => {
        if (!valid) {
          return false
        }
        this.submitData()
      })
    },
    // 校验成功的方法
    async submitData () {
      let res = null
      if (this.formData.id) {
        res = await ApiPutUserEdit(this.formData)
      } else {
        // 默认密码与用户名一致
        this.formData.password = this.formData.username
        res = await ApiPosUser(this.formData)
      }
      if (res.code === 20000) {
        this.$message({
          message: res.message, type: 'success'
        })
        this.handleClose()
      } else {
        this.$message({
          message: '保存失败', type: 'error'
        })
      }
    }
  }
}
</script>


<style>
</style>